/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DetailedCriminal.java
 *
 * Created on Aug 26, 2011, 5:34:10 PM
 */
package Views;

import Controllers.CriminalController;
import Controllers.WantedController;
import Models.Criminal;
import Models.Wanted;
import java.awt.Color;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

/**
 *
 * @author Pan
 */
public class EditWanted extends javax.swing.JDialog {

    int _wantedID;

    /** Creates new form DetailedCriminal */
    public EditWanted(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    public EditWanted(java.awt.Frame parent, boolean modal, Wanted wanted) {
        super(parent, modal);
        initComponents();

        _wantedID = wanted.getID();
        txtCriminalID.setText(String.valueOf(_wantedID));
        spnWantedDate.setValue(wanted.getWantedDate());
        spnBountyMoney.setValue(wanted.getBountyMoney());
        txtSightingPlace.setText(wanted.getSightingPlace());
        if (wanted.getSightingTime() == null) {
            //Nothing happens
        } else {
            spnSightingTime.setEnabled(true);
            spnSightingTime.setValue(wanted.getSightingTime());
        }
        if (!wanted.isInEffect()) {
            cbxInEffect.setSelectedIndex(1);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        btn_Close = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jScrollPanel1 = new javax.swing.JScrollPane();
        txtSightingPlace = new javax.swing.JTextArea();
        jLabel6 = new javax.swing.JLabel();
        spnBountyMoney = new javax.swing.JSpinner();
        spnSightingTime = new javax.swing.JSpinner();
        btn_Submit = new javax.swing.JButton();
        spnWantedDate = new javax.swing.JSpinner();
        jLabel7 = new javax.swing.JLabel();
        cbxInEffect = new javax.swing.JComboBox();
        jLabel8 = new javax.swing.JLabel();
        txtCriminalID = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0), 2));
        jPanel1.setPreferredSize(new java.awt.Dimension(613, 50));

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        jLabel1.setText("Bounty Update Form");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addContainerGap(187, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        btn_Close.setText("Close");
        btn_Close.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_CloseActionPerformed(evt);
            }
        });

        jLabel2.setForeground(new java.awt.Color(255, 51, 51));
        jLabel2.setText("Fugitive's Criminal ID:");

        jLabel3.setForeground(new java.awt.Color(255, 51, 51));
        jLabel3.setText("Issued Date:");

        jLabel4.setForeground(new java.awt.Color(255, 51, 51));
        jLabel4.setText("Bounty Money (INR):");

        jLabel5.setText("Sighting Place:");

        txtSightingPlace.setColumns(20);
        txtSightingPlace.setLineWrap(true);
        txtSightingPlace.setRows(5);
        txtSightingPlace.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtSightingPlaceFocusLost(evt);
            }
        });
        jScrollPanel1.setViewportView(txtSightingPlace);

        jLabel6.setText("Sighting Time:");

        spnBountyMoney.setModel(new javax.swing.SpinnerNumberModel(0, 0, 1000000000, 1));
        spnBountyMoney.setEditor(new javax.swing.JSpinner.NumberEditor(spnBountyMoney, ""));

        spnSightingTime.setModel(new javax.swing.SpinnerDateModel(new java.util.Date(), new java.util.Date(-946796400000L), new java.util.Date(), java.util.Calendar.DAY_OF_MONTH));
        spnSightingTime.setEditor(new javax.swing.JSpinner.DateEditor(spnSightingTime, "MM/dd/yyyy hh:mm:ss"));
        spnSightingTime.setEnabled(false);

        btn_Submit.setText("Submit");
        btn_Submit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_SubmitActionPerformed(evt);
            }
        });

        spnWantedDate.setModel(new javax.swing.SpinnerDateModel(new java.util.Date(), new java.util.Date(-946796400000L), new java.util.Date(), java.util.Calendar.DAY_OF_MONTH));
        spnWantedDate.setEditor(new javax.swing.JSpinner.DateEditor(spnWantedDate, "MM/dd/yyyy"));
        spnWantedDate.setEnabled(false);

        jLabel7.setForeground(new java.awt.Color(255, 51, 51));
        jLabel7.setText("Red Labels: Mandatory fields");

        cbxInEffect.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Yes", "No" }));

        jLabel8.setForeground(new java.awt.Color(255, 51, 51));
        jLabel8.setText("In Effect?");

        txtCriminalID.setEditable(false);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 421, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jLabel2)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(txtCriminalID))
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jLabel3)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(spnWantedDate, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jLabel4)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(spnBountyMoney, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(jLabel8)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cbxInEffect, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(spnSightingTime, javax.swing.GroupLayout.PREFERRED_SIZE, 128, Short.MAX_VALUE))
                    .addComponent(jLabel5)
                    .addComponent(jScrollPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 199, Short.MAX_VALUE))
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel7)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 120, Short.MAX_VALUE)
                .addComponent(btn_Submit)
                .addGap(18, 18, 18)
                .addComponent(btn_Close)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jLabel5)
                    .addComponent(txtCriminalID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(spnWantedDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(spnBountyMoney, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel8)
                            .addComponent(cbxInEffect, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(jScrollPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(spnSightingTime, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btn_Close)
                    .addComponent(btn_Submit)
                    .addComponent(jLabel7))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btn_CloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_CloseActionPerformed
        this.dispose();
    }//GEN-LAST:event_btn_CloseActionPerformed

private void txtSightingPlaceFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtSightingPlaceFocusLost
    if (txtSightingPlace.getText().trim().isEmpty()) {
        spnSightingTime.setEnabled(false);
    } else {
        spnSightingTime.setEnabled(true);
    }
}//GEN-LAST:event_txtSightingPlaceFocusLost

private void btn_SubmitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_SubmitActionPerformed
    Wanted _wanted = new Wanted();
    SimpleDateFormat SightingTimeFormat = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss");
    _wanted.setID(_wantedID);

    _wanted.setBountyMoney(Integer.parseInt(spnBountyMoney.getValue().toString()));
    _wanted.setSightingPlace(txtSightingPlace.getText().trim());
    if (_wanted.getSightingPlace().isEmpty()) {
        _wanted.setSightingTime(null);
    } else {
        String dateString = SightingTimeFormat.format(spnSightingTime.getValue());
        try {
            _wanted.setSightingTime(SightingTimeFormat.parse(dateString));
        } catch (ParseException ex) {
            Logger.getLogger(EditWanted.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    _wanted.setInEffect((cbxInEffect.getSelectedIndex() == 0));
    if (WantedController.EditWanted(_wanted)) {
        JOptionPane.showMessageDialog(null, "Bounty modified.");
    } else {
        JOptionPane.showMessageDialog(null, "Submission failed.");
    }
    this.dispose();
}//GEN-LAST:event_btn_SubmitActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                EditWanted dialog = new EditWanted(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btn_Close;
    private javax.swing.JButton btn_Submit;
    private javax.swing.JComboBox cbxInEffect;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPanel1;
    private javax.swing.JSpinner spnBountyMoney;
    private javax.swing.JSpinner spnSightingTime;
    private javax.swing.JSpinner spnWantedDate;
    private javax.swing.JTextField txtCriminalID;
    private javax.swing.JTextArea txtSightingPlace;
    // End of variables declaration//GEN-END:variables
}
